package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BasicMapper;
import cn.itsource.basic.query.BasicQuery;
import cn.itsource.basic.service.IBasicService;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T>{


    @Autowired
    private BasicMapper<T> basicMapper;

    @Override
    @Transactional
    public void add(T t) {
        basicMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        basicMapper.remove(id);
    }

    @Override
    public T queryById(Long id) {
        return basicMapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return basicMapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BasicQuery query) {
        Integer total = basicMapper.queryCount(query);
        List<T> rows = basicMapper.queryData(query);
        return new PageList<T>(total,rows);
    }

}
